package com.zhangye.test.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhangye.test.mapper.AlipayOrderInfoMapper;
import com.zhangye.test.model.AlipayOrderInfo;
import com.zhangye.test.model.PayList;
import com.zhangye.test.util.OrderInfoUtil2_0;

/**
 * 支付宝
 * 作者： 张叶
 * 创建时间：2018年6月6日  下午2:42:51
 */
@Service
public class AliPayOrderInfoService {

	@Autowired
	private AlipayOrderInfoMapper aliPayOrderInfoMapper;

	@Autowired
	private PayListService payListService;

	public Map<String, String> orderNew(Map<String, Object> param, Long mId) throws ParseException, UnsupportedEncodingException {

		Map<String,String>  result = new HashMap<String,String>();
		Map<String, String> keyValues = new HashMap<String, String>();
		double pay_amount = Double.parseDouble((String) param.get("pay_amount"));
		String timestamp = (String) param.get("timestamp");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int payId = (int) param.get("id");
		String body = "电话沃秀";
		if (param.get("body")!=null&&!param.get("body").equals("")) {
			body = (String) param.get("body");
		}
		
		Map<String, Object> params = OrderInfoUtil2_0.buildOrderParamMap(pay_amount, timestamp.toString(), body);
		Map<String, String> par = (Map<String, String>) params.get("keyValues");
		String out_trade_no = (String) params.get("out_trade_no");
		result.put("status", "1");

		if (params == null || params.equals("")) {
			return null;
		} else {
			//判断金额是否正确
			String where = " and pay_sum = " + pay_amount + " and id = " + payId;
			List<PayList> list = payListService.selectByWhere(where);
			if (list.size() > 0) {
				String sign = OrderInfoUtil2_0.getSign(par);
				
				keyValues.put("app_id", URLEncoder.encode(par.get("app_id"), "UTF-8"));
				keyValues.put("biz_content", URLEncoder.encode(par.get("biz_content"), "UTF-8"));
				keyValues.put("charset", URLEncoder.encode(par.get("charset"), "UTF-8"));
				keyValues.put("method", URLEncoder.encode(par.get("method"), "UTF-8"));
				keyValues.put("sign_type", URLEncoder.encode(par.get("sign_type"), "UTF-8"));
				keyValues.put("timestamp", URLEncoder.encode(par.get("timestamp"), "UTF-8"));
				keyValues.put("version", URLEncoder.encode(par.get("version"), "UTF-8"));
				keyValues.put("notify_url", URLEncoder.encode(par.get("notify_url"), "UTF-8"));
				StringBuilder key = OrderInfoUtil2_0.sort(keyValues);
				System.out.println("key:---------"+key);
				final String orderInfo = key + "&" + sign;
				
				//插入订单信息
				AlipayOrderInfo apo = new AlipayOrderInfo();
				apo.setUserid(mId.intValue());
				apo.setTotalamount(pay_amount);
				apo.setSubject(list.get(0).getPayName());
				apo.setBody(body);
				apo.setOuttradeno(out_trade_no);
				apo.setTimestamp(sdf.parse(timestamp));
				apo.setOrderinfo(orderInfo);
				apo.setResultstatus("");
				apo.setCode("");
				apo.setUpdatetime(null);
				apo.setTradeno("");
				apo.setTradestatus("");
				int month = list.get(0).getGiveSum().intValue();
				apo.setMonthcount(month);
				int i = aliPayOrderInfoMapper.insert(apo);
				if (i==1) {
					System.out.println("orderInfo:----------"+orderInfo);
					result.put("orderInfo", orderInfo);
					result.put("status", "0");
				}
			} else {
				result.put("status", "2");
			}
		}
		return result;
	}
	
}
